/**
 * Copyright (c) 2016 Lenovo Group
 * All Rights Reserved.
 * 
 * Project Name:sdc-core-rest
 * Create Time: 2016年2月9日 下午11:06:45
 */
package com.lenovo.lmrp.base;

import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.HttpResponse;
import org.jboss.resteasy.spi.PropertyInjector;
import org.jboss.resteasy.spi.ResourceFactory;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

import com.google.inject.Provider;

/**
 * 
 * @author wujin
 * @version v1.0
 */
public class GuiceResourceFactory implements ResourceFactory {
    @SuppressWarnings("rawtypes")
    private final Provider provider;
    private final Class<?> scannableClass;
    private PropertyInjector propertyInjector;

    @SuppressWarnings("rawtypes")
    public GuiceResourceFactory(final Provider provider, final Class<?> scannableClass) {
        this.provider = provider;
        this.scannableClass = scannableClass;
    }

    public Class<?> getScannableClass() {
        return scannableClass;
    }

    public void registered(ResteasyProviderFactory factory) {
        propertyInjector = factory.getInjectorFactory().createPropertyInjector(scannableClass, factory);
    }

    public Object createResource(final HttpRequest request, final HttpResponse response,
                                 final ResteasyProviderFactory factory) {
        final Object resource = provider.get();
        propertyInjector.inject(request, response, resource);
        return resource;
    }

    public void requestFinished(final HttpRequest request, final HttpResponse response, final Object resource) {
    }

    public void unregistered() {
    }
}
